package evi.com.evi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import evi.com.evi.R;
import evi.com.evi.utils.AppUtils;
import evi.com.evi.utils.MessageDialog;
import evi.com.evi.utils.ToastUtils;
import evi.com.evi.view.MyGridView;

public class OrderDisposeAdapter<T> extends BaseAdapter {
    private ArrayList<T> mList;
    private Context mContext;
    private int type;    //1-待接单， 2-待取货,  3-已处理

    public OrderDisposeAdapter(Context mContext, ArrayList<T> mList, int type) {
        this.mContext = mContext;
        this.mList = mList;
        this.type = type;
        if (null == this.mList) {
            this.mList = new ArrayList<>();
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_order_dispose,
                        null);
                viewHolder = new ViewHolder();
                viewHolder.tv_order_time = (TextView) convertView.findViewById(R.id.tv_order_time);
                viewHolder.tv_text_1 = (TextView) convertView.findViewById(R.id.tv_text_1);
                viewHolder.tv_text_2 = (TextView) convertView.findViewById(R.id.tv_text_2);
                viewHolder.tv_text_2_2 = (TextView) convertView.findViewById(R.id.tv_text_2_2);
                viewHolder.tv_text_3 = (TextView) convertView.findViewById(R.id.tv_text_3);
                viewHolder.fl_goods = (FrameLayout) convertView.findViewById(R.id.fl_goods);
                viewHolder.gv_goods = (MyGridView) convertView.findViewById(R.id.gv_goods);
                viewHolder.tv_user = (TextView) convertView.findViewById(R.id.tv_user);
                viewHolder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
                viewHolder.tv_trans_type = (TextView) convertView.findViewById(R.id.tv_trans_type);
                viewHolder.tv_peisong_user = (TextView) convertView.findViewById(R.id.tv_peisong_user);
                viewHolder.tv_cancle_reason = (TextView) convertView.findViewById(R.id.tv_cancle_reason);
                viewHolder.tv_refuse_reason = (TextView) convertView.findViewById(R.id.tv_refuse_reason);
                viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
                viewHolder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);
                viewHolder.tv_refuse = (TextView) convertView.findViewById(R.id.tv_refuse);
                viewHolder.tv_btn_manager = (TextView) convertView.findViewById(R.id.tv_btn_manager);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            if (type == 1) {            //未处理-待接单
                viewHolder.tv_text_1.setText("订单号：123456780000");
                viewHolder.tv_text_2.setText("状态：等待商家接单");
                viewHolder.tv_text_3.setText("出发时间：15:00");

                if (viewHolder.tv_text_2.getText().toString().equals("状态：等待商家接单")) {
                    viewHolder.tv_peisong_user.setVisibility(View.GONE);
                    viewHolder.tv_cancle_reason.setVisibility(View.GONE);
                    viewHolder.tv_refuse_reason.setVisibility(View.GONE);
                    viewHolder.tv_btn_manager.setText("确认接单");
                } else {

                }

                //注：其它属性根据订单状态进行选择性的显示和隐藏
            } else if (type == 2) {     //未处理-待取货
                viewHolder.tv_text_1.setText("收货人：吴波");
                viewHolder.tv_text_2.setVisibility(View.GONE);
                viewHolder.tv_text_2_2.setVisibility(View.VISIBLE);
                viewHolder.tv_text_2_2.setText("手机号：15718415612");
                viewHolder.tv_text_3.setText("出发时间：15:00");

                viewHolder.tv_user.setVisibility(View.GONE);
                viewHolder.tv_phone.setVisibility(View.GONE);
                viewHolder.tv_trans_type.setVisibility(View.GONE);
                viewHolder.tv_peisong_user.setVisibility(View.GONE);
                viewHolder.tv_cancle_reason.setVisibility(View.GONE);
                viewHolder.tv_refuse_reason.setVisibility(View.GONE);
                viewHolder.tv_detail.setVisibility(View.GONE);
                viewHolder.tv_refuse.setVisibility(View.GONE);

                viewHolder.tv_btn_manager.setText("已到店取货");

                //注：其它属性根据订单状态进行选择性的显示和隐藏
            } else if (type == 3) {     //已处理
                viewHolder.tv_text_1.setText("订单号：123456780000");
                viewHolder.tv_text_3.setText("出发时间：15:00");
                viewHolder.tv_text_2.setVisibility(View.GONE);
                viewHolder.tv_text_2_2.setVisibility(View.GONE);

                viewHolder.tv_refuse.setVisibility(View.GONE);
                viewHolder.tv_btn_manager.setVisibility(View.GONE);
                //注：其它属性根据订单状态进行选择性的显示和隐藏
            }
            String text = viewHolder.tv_detail.getText().toString().trim();
            setChange(text, viewHolder);

            ArrayList<String> list = new ArrayList<String>();
            list.add("大鸡腿");
            list.add("全家桶");
            list.add("一根油条");
            list.add("炸鱼");
            GridGoodsAdapter goodsAdapter = new GridGoodsAdapter(mContext, list);
            viewHolder.gv_goods.setAdapter(goodsAdapter);

            viewHolder.tv_detail.setOnClickListener(new OnitemClick(viewHolder, position, mList.get(position)));
            viewHolder.tv_refuse.setOnClickListener(new OnitemClick(viewHolder, position, mList.get(position)));
            viewHolder.tv_btn_manager.setOnClickListener(new OnitemClick(viewHolder, position, mList.get(position)));
            return convertView;
        }
    }

    class ViewHolder {
        TextView tv_order_time;        //时间
        TextView tv_text_1;            //首三项
        TextView tv_text_2;
        TextView tv_text_2_2;           //手机号
        TextView tv_text_3;

        TextView tv_user;                //收货人
        TextView tv_phone;                //收货人号码
        TextView tv_trans_type;        //配送方式
        TextView tv_peisong_user;        //配送员
        TextView tv_cancle_reason;    //取消订单原因
        TextView tv_refuse_reason;    //拒绝原因
        TextView tv_money;                //订单金额
        TextView tv_detail;            //隐藏详情
        TextView tv_refuse;            //拒绝
        TextView tv_btn_manager;       //按钮

        FrameLayout fl_goods;            //商品表
        MyGridView gv_goods;      //商品表
    }

    class OnitemClick implements OnClickListener {
        ViewHolder viewHolder;
        int position;
        private Object obj;

        public OnitemClick(ViewHolder viewHolder, int position, Object obj) {
            this.position = position;
            this.viewHolder = viewHolder;
            this.obj = obj;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_detail:        //隐藏详情
                    String text = viewHolder.tv_detail.getText().toString().trim();
                    setChange(text, viewHolder);
                    break;

                case R.id.tv_refuse:        //拒绝
                    if (type == 1) {
                        AppUtils.showMsgDialog(mContext, "拒绝订单原因", "已售罄", "送不过来", 2, new AppUtils.MsgCallBackData() {
                            @Override
                            public void clickMsg(Dialog d, String data) {
                                ToastUtils.showMsg(mContext, "" + data);
                                d.dismiss();
                            }
                        });
                    } else if (type == 2) {
                        AppUtils.showMsgDialog(mContext, new AppUtils.MsgCallBackData() {
                            @Override
                            public void clickMsg(Dialog d, String data) {
                                ToastUtils.showMsg(mContext, "" + data);
                                d.dismiss();
                            }
                        });
                    }
                    break;

                case R.id.tv_btn_manager:        //按钮
                    AppUtils.showMsgDialog(mContext, "确认接单吗？", "确认", "取消", 1, null, new AppUtils.MsgCallBack() {
                        @Override
                        public void confirmMsg(MessageDialog d) {
                            ToastUtils.showMsg(mContext, "接单成功");
                            d.dismiss();
                        }

                        @Override
                        public void cancelMsg(MessageDialog d) {
                            ToastUtils.showMsg(mContext, "接单取消");
                            d.dismiss();
                        }
                    });
                    break;
                default:
                    break;
            }
        }
    }

    private void setChange(String changType, ViewHolder viewHolder) {
        if (changType.equals("展开详情")) {
            viewHolder.tv_detail.setText("隐藏详情");
            viewHolder.tv_user.setVisibility(View.VISIBLE);
            viewHolder.tv_phone.setVisibility(View.VISIBLE);
            viewHolder.tv_trans_type.setVisibility(View.VISIBLE);
            viewHolder.tv_peisong_user.setVisibility(View.VISIBLE);
            viewHolder.tv_cancle_reason.setVisibility(View.VISIBLE);
            viewHolder.tv_refuse_reason.setVisibility(View.VISIBLE);
        } else if (changType.equals("隐藏详情")) {
            viewHolder.tv_detail.setText("展开详情");
            viewHolder.tv_user.setVisibility(View.GONE);
            viewHolder.tv_phone.setVisibility(View.GONE);
            viewHolder.tv_trans_type.setVisibility(View.GONE);
            viewHolder.tv_peisong_user.setVisibility(View.GONE);
            viewHolder.tv_cancle_reason.setVisibility(View.GONE);
            viewHolder.tv_refuse_reason.setVisibility(View.GONE);
        }
    }
}
